<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Login extends CI_Controller {
	public function index() {
		if ($this -> session -> userdata('isLoggedIn')) {
			//redirect('/admin');
			$this -> show_login(false);
		} else {
			$this -> show_login(false);
		}
	}

	function show_login($show_error = false) {
		$data['error'] = $show_error;

		$this -> load -> helper('form');
		$this -> load -> view('login', $data);

	}
	var $details;
	function login_user() {
		// Create an instance of the user model
		$this -> load -> model('m_login');

		// Grab the email and password from the form POST
		$email = $this -> input -> post('email');
		$pass = $this -> input -> post('password');

		//Ensure values exist for email and pass, and validate the user's credentials
		if ($email && $pass && $this -> m_login -> validate_user($email, $pass)) {
			// If the user is valid, redirect to the main view
			$details['email']=$email;
			$details['password']=$pass;
			$details['rsid']=$this -> m_login -> getrsidbyemail($email);
			//echo $details['rsid']; exit();
			
			$this -> session -> set_userdata(
			array(

				'email' => $email, 
				'rsid' => $this -> m_login -> getrsidbyemail($email),
				'isLoggedIn' => true));
				//echo $this -> session -> userdata('isLoggedIn');exit();
			//$this -> set_session();
			
			redirect('/admin');
		} else {
			// Otherwise show the login screen with an error message.
			$this -> show_login(true);
		}
	}


	function set_session() {
		// session->set_userdata is a CodeIgniter function that
		// stores data in a cookie in the user's browser.  Some of the values are built in
		// to CodeIgniter, others are added (like the IP address).  See CodeIgniter's documentation for details.
		$this -> session -> set_userdata(
			array(

				'email' => $details['email'], 
				'rsid' => $details['rsid'],
				'isLoggedIn' => true));
				//echo $this -> session -> userdata('isLoggedIn');exit();
	}
	
}
